package com.xkdx.guangguang.pushserviceforSelf;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.TelephonyManager;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.HomeActivity;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class PushServiceForSelf extends Service {
	private PushServiceForSelfAction action;
	private PushServiceForSelfModule module;
	private PushServiceForSelfPresistence presistence;
	private TelephonyManager mTelephonyManager;
	private Timer mTimer;
	private static int PUSH_ID = 20;
	private Notification notification;
	private NotificationManager mNotificationManager;
	private List<PushForSelfInfo> pushInfoselfList;
	private SharePrefenceUtil sp;
	private String userId;
	private String userToken;

	@Override
	public void onCreate() {

		super.onCreate();
		sp = new SharePrefenceUtil(getApplicationContext(), IConstants.SP_USER);

		mTimer = new Timer();
		mTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				userId = sp.getUserID();
				userToken = sp.getUserLoginToken();
				if (isInTimePeriod() && !("".equals(userId))
						&& !("".equals(userToken))) {
					mHandler.sendEmptyMessage(0);
				}
			}
		}, 10000, 60 * 60 * 1000);

	}

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 0) {
				// Toast.makeText(getApplicationContext(), "循环", 0).show();
				getPushInfo();
			}
		};
	};

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	public class LocalBinder extends Binder {
		// 返回本地服务
		PushServiceForSelf getService() {
			return PushServiceForSelf.this;
		}
	}

	private void getPushInfo() {

		action = new PushServiceForSelfAction(userId, userToken);
		// action = new PushServiceForSelfAction("1290",
		// "855de6e23b8b7489e95d638d8c939be7");

		module = new PushServiceForSelfModule();
		presistence = new PushServiceForSelfPresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();
	}

	protected void showPush(HashMap<String, AbsModule> map) {
		if (map.containsKey("0")) {
			pushInfoselfList = ((PushServiceForSelfModule) map.get("0")).pushInfoList;

			if (pushInfoselfList != null && pushInfoselfList.size() > 0) {
				String ns = Context.NOTIFICATION_SERVICE;
				mNotificationManager = (NotificationManager) getSystemService(ns);
				for (int i = 0; i < pushInfoselfList.size(); i++) {

					IConstants.isFromSelf = true;

					setNotification(pushInfoselfList.get(i), i);

				}
			}

		}

	}

	private boolean isInTimePeriod() {

		Calendar cal = Calendar.getInstance();// 当前日期
		int hour = cal.get(Calendar.HOUR_OF_DAY);// 获取小时
		int minute = cal.get(Calendar.MINUTE);// 获取分钟
		int minuteOfDay = hour * 60 + minute;// 从0:00分开是到目前为止的分钟数
		final int start = 10 * 60;// 起始时间 10:00的分钟数
		final int end = 21 * 60;// 结束时间 19:00的分钟数
		if (minuteOfDay >= start && minuteOfDay <= end) {
			return true;
		}

		return false;
	}

	private void setNotification(PushForSelfInfo pushInfo, int i) {
		int icon = R.drawable.main;
		CharSequence tickerText = getString(R.string.push_name);
		long longwhen = System.currentTimeMillis();

		notification = new Notification(icon, tickerText, longwhen);

		Intent intent = new Intent(this, HomeActivity.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("pushself", pushInfo);
		intent.putExtras(bundle);
		// IConstants.objectType = pushInfo.getObjectType();
		// IConstants.objectValue = pushInfo.getObjectValue();
		PendingIntent pi = PendingIntent.getActivity(this, i, intent, 0);
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		notification.setLatestEventInfo(this, getString(R.string.push_notify),
				pushInfo.getMessage(), pi);
		mNotificationManager.notify(PUSH_ID + i, notification);
	}

}
